package cn.edu.nju.software.bean.show;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import cn.edu.nju.software.bean.storage.Comment;

public class CommentShow {

	private int id;
	private UserShow user;
	private BookShow book;
	private String content;
	private String postTime;
	private String stickyTime;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");

	public CommentShow(Comment comment, UserShow user, BookShow book) {
		if (comment != null) {
			this.id = comment.getId();
			this.content = comment.getContent();
			if (comment.getPostTime() != null) {
				this.postTime = dateFormat.format(comment.getPostTime());
			}
			if (comment.getStickyTime() != null) {
				this.stickyTime = dateFormat.format(comment.getStickyTime());
			}
		}
		this.user = user;
		this.book = book;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public UserShow getUser() {
		return user;
	}

	public void setUser(UserShow user) {
		this.user = user;
	}

	public BookShow getBook() {
		return book;
	}

	public void setBook(BookShow book) {
		this.book = book;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getPostTime() {
		return postTime;
	}

	public void setPostTime(String postTime) {
		this.postTime = postTime;
	}

	public void setPostTime(Timestamp postTime) {
		if (postTime != null) {
			this.postTime = dateFormat.format(postTime);
		}
	}

	public String getStickyTime() {
		return stickyTime;
	}

	public void setStickyTime(String stickyTime) {
		this.stickyTime = stickyTime;
	}

	public void setStickyTime(Timestamp stickyTime) {
		if (stickyTime != null) {
			this.stickyTime = dateFormat.format(stickyTime);
		}
	}
	
	public Comment getComment() {
		try {
			Timestamp postTime = null, stickyTime = null;
			if (this.postTime != null) {
				postTime = new Timestamp(dateFormat.parse(this.postTime).getTime());
			}
			if (this.stickyTime != null) {
				stickyTime = new Timestamp(dateFormat.parse(this.stickyTime).getTime());
			}
			return new Comment(id, user.getUserID(), book.getBookID(), content, postTime, stickyTime);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
